<?php
session_cache_expire(999999999);
if(!isset($_SESSION)) 
{ 
	session_start(); 
}
/*if (isset($_SESSION['user']) && isset($_SESSION['mysheets']) && is_array($_SESSION['mysheets'])) {
	include 'CheckTime.php';
	include '../operations/connection.php';
	$usercreate = $_SESSION['IDEmployee'];
	$queryInsert = "insert into sa_check_time(employeeID,employeeName,time_work,time_in,time_out,is_late,is_comeback_early,half_day_off,all_day_off,overtime,note,user_create,createDate)
values";
	$tempsql = "";
	foreach ($_SESSION['mysheets'] as $sheetkey =>  $sheetobject){
		foreach ($sheetobject as $key =>  $object){
			$CT = unserialize (serialize ($object));
			$tempsql .= sprintf("(%s,'%s','%s','%s','%s',%s,%s,%s,%s,%s,'%s',%s,NOW()),",$CT->employeeID,$CT->employeeName,$CT->date,$CT->inTime,$CT->outTime,$CT->late,$CT->early,$CT->halfDayOff,$CT->aDayOff,$CT->overtime,$CT->note,$usercreate);

		}
	}
	if ($tempsql != "") {
		$tempsql = substr($tempsql,0, -1);
	}
	mysql_query($queryInsert.$tempsql);
	unset($_SESSION['mysheets']);
	mysql_close($connection);
}*/
$lang=$_SESSION['lang'];
if($lang=='')
	$lang='EN';


if (isset($_SESSION['user']) && isset($_SESSION['rs_mysheets']) && is_array($_SESSION['rs_mysheets'])) {
	$mkq=$_SESSION['rs_mysheets'];
	$slm=count($mkq);
	if($slm>0){
		include 'CheckTime.php';
		include '../operations/connection.php';
		$usercreate = $_SESSION['IDEmployee'];
		$queryInsert = "insert into sa_check_time(employeeID,employeeName,time_work,time_in,time_out,is_late,is_comeback_early,half_day_off,all_day_off,overtime,note,user_create,createDate)
	values ";
		$tempsql = "";
		foreach ($_SESSION['rs_mysheets'] as $sheetkey =>  $sheetobject){
			$CT = unserialize (serialize ($sheetobject));
			$s1="DELETE from sa_check_time where sa_check_time.time_work='".$CT->date."' and sa_check_time.employeeID='".$CT->employeeID."' and sa_check_time.`status`=0";
			mysql_query($s1);
			if($tempsql==""){
				$tempsql ="('".$CT->employeeID."','".$CT->employeeName."','".$CT->date."','".$CT->inTime."','".$CT->outTime."',".$CT->late.",".$CT->early.",".$CT->halfDayOff.",".$CT->aDayOff.",".$CT->overtime.",'".$CT->note."','".$usercreate."',now())";
			}else{
				$tempsql .=",('".$CT->employeeID."','".$CT->employeeName."','".$CT->date."','".$CT->inTime."','".$CT->outTime."',".$CT->late.",".$CT->early.",".$CT->halfDayOff.",".$CT->aDayOff.",".$CT->overtime.",'".$CT->note."','".$usercreate."',now())";
			}
			
		}
		mysql_query($queryInsert.$tempsql);
		
		$addressbrowser = $_SERVER['SERVER_NAME'];
		$temp = explode(".", $addressbrowser);
		$url = 'http://1.234.53.52/mail/sendmail_checktime.php';
		//$url = 'http://localhost/mail/sendmail_checktime.php';
		$fields = array(
								'user_name' => urlencode($_SESSION['fullname']),
								'company' => urlencode($temp[0]),
								'lang' => urlencode($lang),
						);

		//url-ify the data for the POST
		foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
		rtrim($fields_string, '&');
		
		//open connection
		$ch = curl_init();
		
		//set the url, number of POST vars, POST data
		curl_setopt($ch,CURLOPT_URL, $url);
		curl_setopt($ch,CURLOPT_POST, count($fields));
		curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
		
		//execute post
		$result = curl_exec($ch);
		
		//close connection
		curl_close($ch);
		unset($_SESSION['rs_mysheets']);
		unset($_SESSION['mysheets']);
		mysql_close($connection);
	}else{
		unset($_SESSION['rs_mysheets']);
		unset($_SESSION['mysheets']);
	}
}

header("Location: ../salary.php?content=import_checktime");

?>